package com.xintonghua.bussiness.ui.user;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.MyTimeUtils;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.OrderHomeAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.ShopOrderListBean;
import com.xintonghua.bussiness.bean.StockOrderResponse;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.addapp.pickers.picker.DatePicker;

/**
 * 作者：wanxianyi
 * 时间：2018/3/5.
 * 包名：com.xintonghua.bussiness.ui.user
 */

public class HistoryAcitivity extends BaseActivity {
    @BindView(R.id.lv_history)
    XRecyclerView lvHistory;
    OrderHomeAdapter adapter;
    @BindView(R.id.tv_select_time)
    TextView tvSelectTime;
    private int page = 1;
    private String time;
    ShopOrderListBean response;
    List<ShopOrderListBean.ListBean> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("历史订单");
        time = MyTimeUtils.getYYMMDDL(System.currentTimeMillis());
        tvSelectTime.setText(time);
        RefreshUtils.initList(this, lvHistory, MyUtils.dip2px(this, 8), R.color.grey_bg);
        adapter = new OrderHomeAdapter(this, list);
        lvHistory.setAdapter(adapter);
        lvHistory.setLoadingListener(this);
        find();
    }

    private void find() {
        httpCent.recordsConsumption(page, MyState.SIZE, time, this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                if (page == 1) {
                    list.clear();
                }
                response = JSONObject.parseObject(data.toString(), ShopOrderListBean.class);
                list.addAll(response.getList());
                adapter.notifyDataSetChanged();
                RefreshUtils.onRefresh(adapter, lvHistory);
                break;
        }
    }

    @OnClick({R.id.tv_select_time})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_select_time:
                onYearMonthDayPicker(view);
                break;
        }
    }

    public void onYearMonthDayPicker(View view) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        final DatePicker picker = new DatePicker(this);
        picker.setCanLoop(true);
        picker.setWheelModeEnable(true);
        picker.setTopPadding(15);
        picker.setRangeStart(1790, 8, 29);
        picker.setRangeEnd(2111, 1, 11);
        picker.setSelectedItem(year, month, day);
        picker.setWeightEnable(true);
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                time = year + "-" + month + "-" + day;
                tvSelectTime.setText(time);
                find();
            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {

            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();
    }

    @Override
    public void onRefresh() {
        page = 1;
        find();
    }

    @Override
    public void onLoadMore() {
        if (RefreshUtils.hasNext(response, adapter, lvHistory)) {
            page++;
            find();
        }
    }
}
